import { AttackCard } from "../cards/AttackCard";
import { ControlCard } from "../cards/ControlCard";
import { End } from "../cards/End";
import { HPCard } from "../cards/HPCard";
import { MagicAttack } from "../cards/MagicAttack";
import { MagicDefense } from "../cards/MagicDefense";
import { MaxHPCard } from "../cards/MaxHPCard";
import { PhysicsAttack } from "../cards/PhysicsAttack";
import { PhysicsDefense } from "../cards/PhysicsDefense";
import { ShieldCard } from "../cards/ShieldCard";
import { StudyMethodCard } from "../cards/StudyMethodCard";
import { threeCard } from "../cards/ThreeCard";
import { Card, CardSubClass } from "../class/Card";
import { CardNotFindError } from "../class/errors";

export class CardFactory {
    static cards: typeof Card[] = [AttackCard, HPCard, MagicAttack, MagicDefense, PhysicsAttack, PhysicsDefense, ShieldCard, MaxHPCard, End, StudyMethodCard, ControlCard, threeCard]
    static add(cardClass: typeof Card) {
        this.cards.push(cardClass)
    }
    static getByName(cardName?: string) {
        if (cardName === undefined) {
            const sumRandom = this.cards.map(e => e.random).reduce((p, c) => p + c)
            let rand = Math.floor(Math.random() * sumRandom);
            for (let i = 0; i < this.cards.length; i++) {
                if (rand <= this.cards[i].random) {
                    return this.cards[i] as unknown as typeof CardSubClass;
                } else {
                    rand -= this.cards[i].random
                }
            }
            throw new Error("随机拿牌出错。")
        } else {
            const _CardSubClass = this.cards.find(e => e.cardName === cardName);
            if (_CardSubClass === undefined) {
                throw new CardNotFindError();
            } else {
                return _CardSubClass as unknown as typeof CardSubClass;
            }
        }
    }
    static getAllCardName() {
        return this.cards.map(CardClass => CardClass.cardName)
    }
}